package com.soo.hoo.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soo.hoo.admin.mapper.SysRolePermissionMapper;
import com.soo.hoo.admin.model.entity.SysRolePermission;
import com.soo.hoo.admin.service.RolePermissionService;
import com.soo.hoo.core.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 角色权限关联接口服务
 * </p>
 *
 * @author 杨燚
 * @since 2023-04-11
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements RolePermissionService {

    @Override
    public void saveRelevance(Long roleId, List<Long> permissionIds) {
        if (CollUtil.isEmpty(permissionIds)){
            return;
        }
        List<SysRolePermission> rps = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            SysRolePermission rp = new SysRolePermission();
            rp.setRoleId(roleId);
            rp.setPermissionId(permissionId);
            rps.add(rp);
        }
        baseMapper.insertBatchSomeColumn(rps);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        LambdaQueryWrapper<SysRolePermission> lqw = new LambdaQueryWrapper<SysRolePermission>()
                .eq(SysRolePermission::getRoleId, roleId);
        baseMapper.delete(lqw);
    }

    @Override
    public List<SysRolePermission> listByRoleIds(Collection<?> roleIds) {
        LambdaQueryWrapper<SysRolePermission> lqw = new LambdaQueryWrapper<SysRolePermission>()
                .in(SysRolePermission::getRoleId, roleIds);
        return baseMapper.selectList(lqw);
    }
}
